from flask import Flask, jsonify
from flask_restful import Resource, Api, reqparse
import logging
import logging.config
import sys
import yaml
from nlp import NLPService


NLP_SERVICE = 'nlp_service'
USER_DICT = 'user_dict'
MODEL_PATH = 'model_path'
KEYWORD_METHOD = 'keyword_method'
APP_SERVE = 'app_serve'


ARG_TEXT = 'text'
ARG_TOP_K = 'keyword_top_k'
ARG_TOP_N = 'keyword_cloud_n'
ARG_ALLOW_POS = 'keyword_allow_pos'
ARG_TF = 'keyword_tf'


def load_yaml(yaml_file):
    with open(yaml_file, 'r') as infile:
        config = yaml.load(infile)
    return config


def set_logging(conf_file):
    config = load_yaml(conf_file)
    logging.config.dictConfig(config)


class NLPResource(Resource):

    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument(ARG_TEXT, type=str, location='json')
        self.parser.add_argument(ARG_TOP_K, type=int, location='json')
        self.parser.add_argument(ARG_TOP_N, type=int, location='json')
        self.parser.add_argument(ARG_ALLOW_POS, type=list, location='json')
        # self.parser.add_argument(ARG_TF, type=bool, location='json')

    def get(self):
        args = self.parser.parse_args()
        logger.debug('args: %s' % str(args))
        try:
            res = self._process(args)
            return jsonify({'result': res})
        except Exception as ex:
            return jsonify({'message': 'Error %s' % str(ex)})

    def post(self):
        args = self.parser.parse_args()
        logger.debug('args: %s' % str(args))
        try:
            res = self._process(args)
            return jsonify({'result': res})
        except Exception as ex:
            return jsonify({'message': 'Error %s' % str(ex)})

    def _process(self, args):
        raise NotImplementedError()


class KeywordCloudResource(NLPResource):
    def _process(self, args):
        text = args[ARG_TEXT]
        keyword_top_k = args[ARG_TOP_K]
        top_n = args[ARG_TOP_N]
        keyword_allow_pos = args[ARG_ALLOW_POS]
        # keyword_tf = args[ARG_TF]
        keywords = nlp_service.get_keywords(text, keyword_top_k, keyword_allow_pos)
        return nlp_service.get_similar_words(keywords, topn=top_n)


class KeywordResource(NLPResource):

    def _process(self, args):
        text = args[ARG_TEXT]
        keyword_top_k = args[ARG_TOP_K]
        keyword_allow_pos = args[ARG_ALLOW_POS]
        # keyword_tf = args[ARG_TF]
        return nlp_service.get_keywords(text, keyword_top_k, keyword_allow_pos)


config_file = '/home/alpha/youka/youka-search-api/config/config.yml'
logging_config_file = '/home/alpha/youka/youka-search-api/config/logging.yml'

set_logging(logging_config_file)

config = load_yaml(config_file)[NLP_SERVICE]

model_path = config[MODEL_PATH]
keyword_method = config[KEYWORD_METHOD]
user_dict = config[USER_DICT]
app_serve = config[APP_SERVE]

logger = logging.getLogger()
nlp_service = NLPService(model_path, user_dict, keyword_method)
print(nlp_service.get_keywords("女装", 10, []))
print(nlp_service.get_similar_words(['女装'], 10))